
from os import remove
import PySide6
from PySide6 import QtCore, QtWidgets, QtGui
from PySide6.QtWidgets import QGraphicsScene, QGraphicsItem
from PySide6.QtCore import QRectF, QPointF, QTimer
from PySide6.QtGui import Qt

from my_graphics_view import MyGraphicsView
from item.rect_Item import RectItem
from item.item_type import itemType

class MyGraphicsScene(QGraphicsScene):
    
    def __init__(self, parent = None):
        self.__needNewItem = None # 需要新增的图形项
        super().__init__(parent)
        self.__timer = QTimer()
        self.setSceneRect(0, 0, 800, 480)
        self.__timer.timeout.connect(self.updateSceneSlot)
        self.__timer.start(200) # 200ms定时


    '''设置需要新增的图形项'''
    def setNewItem(self, newItemType: itemType) -> None:
        if newItemType == itemType.NONE_ITEM :
            return
        elif newItemType == itemType.RECTANGLE:
            self.__needNewItem = RectItem()
        else :
            return

        views = self.views()
        for i in views:
            if(True == isinstance(i, MyGraphicsView) ):
                i.setMouseShape(Qt.CursorShape.CrossCursor)


    '''重写鼠标按下事件'''
    def mousePressEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        startPos = QPointF()   # 鼠标按下的位置（场景坐标） 
        # self.update()   # 每次按下都刷新一下整个场景
        if event.button() != Qt.MouseButton.LeftButton or self.__needNewItem == None:    # 如果不是左键按下或者就直接返回，不进行处理
            return super().mousePressEvent(event)

        startPos = event.scenePos()
        # 刚开始按下，判断是否需要新建图形项
        if self.__needNewItem.TYPE == itemType.RECTANGLE and self.__needNewItem.scene() == None:
            self.addItem(self.__needNewItem)   # 将图形项加入场景中
            self.__needNewItem.setPos(startPos - QPointF(self.__needNewItem.size().width() / 2.0, 
                                                        self.__needNewItem.size().height() / 2.0) )

        return super().mousePressEvent(event)


    def mouseMoveEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        delta = QPointF()
        curPos = event.scenePos()
        # 在新建图形项时，若拖动鼠标，则可以改变图形项的大小
        if self.__needNewItem != None and self.__needNewItem.scene() == self:
            delta = curPos - self.__needNewItem.pos()
            # print(delta, ", ", curPos, ", ", self.__needNewItem.pos())
            self.__needNewItem.resize(delta.x() * 2, delta.y() * 2)
            self.update()

        return super().mouseMoveEvent(event)

    def mouseReleaseEvent(self, event: PySide6.QtWidgets.QGraphicsSceneMouseEvent) -> None:
        if self.__needNewItem == None :
            return super().mouseReleaseEvent(event)
        # 松开鼠标，则图形项新增完成
        self.__needNewItem.setFlag(QGraphicsItem.ItemIsMovable)     # 让鼠标可以拖动图形项
        self.__needNewItem = None
        views = self.views()
        for i in views:
            if(True == isinstance(i, MyGraphicsView) ):
                i.setMouseShape(Qt.CursorShape.ArrowCursor)

        return super().mouseReleaseEvent(event)
    
    '''移除图形项槽函数'''
    @QtCore.Slot(QGraphicsItem)
    def removeItemSlot(self, item: QGraphicsItem):
        self.removeItem(item)

    '''刷新场景槽函数'''
    @QtCore.Slot()
    def updateSceneSlot(self):
        self.update()